//
//  ForgetSetPassVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "ForgetSetPassVC.h"

@interface ForgetSetPassVC ()
@property (nonatomic,strong) UILabel        * nameLabel;
@property (nonatomic,strong) UILabel        * infoLabel;
@property (nonatomic,strong) UITextField    * passTextField;
@property (nonatomic,strong) UIButton       * passBtn;
@property (nonatomic,strong) UITextField    * passAgainTextField;
@property (nonatomic,strong) UIButton       * passAgainBtn;
@property (nonatomic,strong) UIButton       * enterBtn;

@end

@implementation ForgetSetPassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";
   
    [self setSubViewLayout];
}

-(void)setSubViewLayout{
    
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.infoLabel];
    [self.view addSubview:self.passTextField];
    [self.view addSubview:self.passAgainTextField];
    [self.view addSubview:self.enterBtn];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)passBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.passTextField.secureTextEntry = NO;
    }else{
        self.passTextField.secureTextEntry = YES;
    }
}

-(void)passAgainBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.passAgainTextField.secureTextEntry = NO;
    }else{
        self.passAgainTextField.secureTextEntry = YES;
    }
}

-(void)enterBtnAction{
        
    if ([self.passTextField.text isEqual:@""]) {
        FhudShowStr(@"请输入密码");
        return;
    }
    
    if (self.passTextField.text.length < 8) {
        FhudShowStr(@"密码长度在8到16个字符");
        return;
    }
    
    if (![self.passTextField.text containsAlphabetAndNumber]) {
        FhudShowStr(@"密码必须包含数字和英文字符");
        return;
    }
    
    if ([self.passAgainTextField.text isEqual:@""]) {
        FhudShowStr(@"请确认密码");
        return;
    }
    
    if (![self.passTextField.text isEqual:self.passAgainTextField.text]) {
        FhudShowStr(@"两次密码输入不一致，请重新输入");
        return;
    }
    NSDictionary * parms = @{@"phone":self.phone ?:@"",@"password":self.passTextField.text ?:@"",@"sm2Encrypt":self.sm2Encrypt};
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"auth-center/api/log/resetPassword") params:parms success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        FhudShowStr(@"修改成功")
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
    }];
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 * FMScaleX, NavHeight + 12 * FMScaleX, 150 * FMScaleX, 45 * FMScaleX)];
        _nameLabel.textColor = RDRGB(0x714933);
        _nameLabel.font = Bold(32);
        _nameLabel.text = @"重置密码";
    }
    return _nameLabel;
}

-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(GETMinX(self.nameLabel), GETMaxY(self.nameLabel) + 5 * FMScaleX, 180 * FMScaleX, 20 * FMScaleX)];
        _infoLabel.font = Regular(14);
        _infoLabel.textColor = RDRGB(0x714933);
        _infoLabel.text = @"中医养生戒指  助您酣然入睡";
    }
    return _infoLabel;
}

-(UITextField *)passTextField{
    
    if (!_passTextField) {
        _passTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.infoLabel) + 100 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _passTextField.font = Regular(16);
        _passTextField.textColor = RDRGB(0x714933);
        _passTextField.secureTextEntry = YES;
        _passTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请设置登录密码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _passTextField.rightViewMode = UITextFieldViewModeAlways;
        _passTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.passBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _passTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_passTextField addSubview:line];
        _passTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passTextField;
}

-(UIButton *)passBtn{
    
    if (!_passBtn) {
        _passBtn = [[UIButton alloc]initWithFrame:CGRectMake(65, 5 * FMScaleX, 30 * FMScaleX, 26 * FMScaleX)];
        [_passBtn setImage:[UIImage imageNamed:@"Login_pass_icon"] forState:UIControlStateNormal];
        [_passBtn setImage:[UIImage imageNamed:@"Login_pass_icon_sel"] forState:UIControlStateSelected];
        [_passBtn addTarget:self action:@selector(passBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passBtn;
}

-(UITextField *)passAgainTextField{
    
    if (!_passAgainTextField) {
        _passAgainTextField = [[UITextField alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.passTextField) + 30 * FMScaleX, WIDTH - 60 * FMScaleX, 40 * FMScaleX)];
        _passAgainTextField.font = Regular(16);
        _passAgainTextField.textColor = RDRGB(0x714933);
        _passAgainTextField.secureTextEntry = YES;
        _passAgainTextField.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"确认登录密码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
            string;
        });
        _passAgainTextField.rightViewMode = UITextFieldViewModeAlways;
        _passAgainTextField.rightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 102*FMScaleX, 36*FMScaleX)];
            [view addSubview:self.passAgainBtn];
            view;
        });
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 45 * FMScaleX, _passAgainTextField.getWidth, 1)];
        line.backgroundColor = RDRGB(0xCDC1B8);
        [_passAgainTextField addSubview:line];
        _passAgainTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passAgainTextField;
}

-(UIButton *)passAgainBtn{
    
    if (!_passAgainBtn) {
        _passAgainBtn = [[UIButton alloc]initWithFrame:CGRectMake(65, 5 * FMScaleX, 30 * FMScaleX, 26 * FMScaleX)];
        [_passAgainBtn setImage:[UIImage imageNamed:@"Login_pass_icon"] forState:UIControlStateNormal];
        [_passAgainBtn setImage:[UIImage imageNamed:@"Login_pass_icon_sel"] forState:UIControlStateSelected];
        [_passAgainBtn addTarget:self action:@selector(passAgainBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passAgainBtn;
}

-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(30 * FMScaleX, GETMaxY(self.passAgainTextField) + 50 * FMScaleX, WIDTH - 60 * FMScaleX, 50 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Login_enter_icon"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Bold(20);
    }
    return _enterBtn;
}
@end
